package com.wyp168.renrenfilm.core.service.impl;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyp168.common.utils.PageUtils;
import com.wyp168.common.utils.Query;

import com.wyp168.renrenfilm.core.dao.WatchDao;
import com.wyp168.renrenfilm.core.entity.WatchEntity;
import com.wyp168.renrenfilm.core.service.WatchService;


@Service("watchService")
@Slf4j
public class WatchServiceImpl extends ServiceImpl<WatchDao, WatchEntity> implements WatchService {

    @Override
    public PageInfo<WatchEntity> queryPage(Map<String, String> params) {
        int page = 1;
        int size = 10;

        if (Objects.nonNull(params.get("page"))) {
            page = Integer.parseInt(params.get("page"));
        }
        if (Objects.nonNull(params.get("limit"))) {
            size = Integer.parseInt(params.get("limit"));
        }

        QueryWrapper<WatchEntity> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(params.get("key"))) {
            wrapper.like("watcher", (String) params.get("key"));
        }

        PageMethod.startPage(page, size);
        List<WatchEntity> watchEntities = baseMapper.selectList(wrapper);


        return new PageInfo<WatchEntity>(watchEntities);
    }

    @Override
    public void saveWatcher(WatchEntity watch) {
        watch.setStartTime(LocalDateTime.now());

        baseMapper.insert(watch);
    }

}